import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: () => import('@/components/Login.vue') },
    {
      path: '/',
      redirect: '/user',
      component: () => import('@/components/Home.vue'),
      children: [
        { path: 'user', component: () => import('@/components/menus/User.vue') },
        { path: 'good', component: () => import('@/components/menus/Good.vue') },
        { path: 'order', component: () => import('@/components/menus/Order.vue') },
        { path: 'right', component: () => import('@/components/menus/Right.vue') },
        { path: 'setting', component: () => import('@/components/menus/Setting.vue') },
        // 因为路由详情也是要展示到这里那么就是二级路由出口处
        { path: '/detail/:id', component: () => import('@/components/user/UserDetail.vue') }
      ]
    }

  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
